package cn.edu.zjicm.wordsnet_d.fragment;

import android.content.Context;
import android.os.Bundle;
import android.os.Handler;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.ViewFlipper;
import cn.edu.zjicm.wordsnet_d.R;
import cn.edu.zjicm.wordsnet_d.activity.ExamRunActivity;
import cn.edu.zjicm.wordsnet_d.data.OneWord;
import cn.edu.zjicm.wordsnet_d.data.RelationShip;
import cn.edu.zjicm.wordsnet_d.data.test.selection.Cloze;
import cn.edu.zjicm.wordsnet_d.datapersistent.AppPreference;
import cn.edu.zjicm.wordsnet_d.db.WordFactory;
import cn.edu.zjicm.wordsnet_d.media.VoicePlayer;
import cn.edu.zjicm.wordsnet_d.util.NightModeUtil;
import cn.edu.zjicm.wordsnet_d.util.Utils;

public class ExamRunMode34Fragment extends Fragment implements OnClickListener {

	public ExamRunMode34Fragment(RelationShip relationShip, int testMode,
			ExamRunActivity examRunActivity) {
		this.relationShip = relationShip;
		this.testMode = testMode;
		this.mExamRunActivity = examRunActivity;
	}

	public ExamRunMode34Fragment() {

	}

	private ExamRunActivity mExamRunActivity;
	// Data
	private RelationShip relationShip;
	private OneWord word;
	private int testMode;

	// View
	private TextView mRelationShipText1, mRelationShipText2;
	private LinearLayout rightContainer, wrongContainer; // 显示单词详情页面正确和错误单词
	private LinearLayout wordErrorContainer, wordErrorContainerParent; // 选择题页面错误单词

	private LinearLayout radioLayout[] = new LinearLayout[4];
	private TextView radioText[] = new TextView[4];
	private TextView radioButton[] = new TextView[4];
	private ImageView resultImage[] = new ImageView[4];
	//
	private Button hintButton, nextButton;
	private ViewFlipper displayFlipper;// , buttonFlipper;
	private Animation pushAnimation;

	private WordFactory wf;
	private LayoutInflater mInflater;
	private Cloze qmaker;

	int optionABCD; // 随机设置选项A,B,C,D
	boolean everforget;
	private int[] testBgs;
	private int radioTextColor;
	private int radioWrongColor;
	private OneWord[] radioWord = new OneWord[4];

	Handler handler = new Handler();

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		// return null;
		View v = inflater.inflate(R.layout.fragment_exam_run_mode34, null);
		mRelationShipText1 = (TextView) v.findViewById(R.id.relationship_text1);
		mRelationShipText2 = (TextView) v.findViewById(R.id.relationship_text2);
		rightContainer = (LinearLayout) v
				.findViewById(R.id.word_right_container);
		wrongContainer = (LinearLayout) v
				.findViewById(R.id.word_wrong_container);
		wordErrorContainer = (LinearLayout) v
				.findViewById(R.id.word_error_container);
		wordErrorContainerParent = (LinearLayout) v
				.findViewById(R.id.word_error_container_parent);

		hintButton = (Button) v.findViewById(R.id.test_hint_button);
		nextButton = (Button) v.findViewById(R.id.test_next_button);
		displayFlipper = (ViewFlipper) v.findViewById(R.id.diplay_flipper);
		// buttonFlipper = (ViewFlipper) v.findViewById(R.id.button_flipper);

		radioLayout[0] = (LinearLayout) v.findViewById(R.id.test_answer_a);
		radioLayout[1] = (LinearLayout) v.findViewById(R.id.test_answer_b);
		radioLayout[2] = (LinearLayout) v.findViewById(R.id.test_answer_c);
		radioLayout[3] = (LinearLayout) v.findViewById(R.id.test_answer_d);
		radioText[0] = (TextView) v.findViewById(R.id.radio_text_num1);
		radioText[1] = (TextView) v.findViewById(R.id.radio_text_num2);
		radioText[2] = (TextView) v.findViewById(R.id.radio_text_num3);
		radioText[3] = (TextView) v.findViewById(R.id.radio_text_num4);
		radioButton[0] = (TextView) v.findViewById(R.id.radio_button_num1);
		radioButton[1] = (TextView) v.findViewById(R.id.radio_button_num2);
		radioButton[2] = (TextView) v.findViewById(R.id.radio_button_num3);
		radioButton[3] = (TextView) v.findViewById(R.id.radio_button_num4);
		resultImage[0] = (ImageView) v.findViewById(R.id.answer_image_a);
		resultImage[1] = (ImageView) v.findViewById(R.id.answer_image_b);
		resultImage[2] = (ImageView) v.findViewById(R.id.answer_image_c);
		resultImage[3] = (ImageView) v.findViewById(R.id.answer_image_d);

		wf = WordFactory.getInstance(getActivity());
		mInflater = (LayoutInflater) getActivity().getSystemService(
				Context.LAYOUT_INFLATER_SERVICE);
		qmaker = new Cloze(getActivity());

		testBgs = new int[3];
		if (NightModeUtil.isNightModeNow()) {
			testBgs[0] = R.drawable.rectangle_test_bg1;
			testBgs[1] = R.drawable.rectangle_test_bg2;
			testBgs[2] = R.drawable.rectangle_test_bg3;
			radioTextColor = R.color.word_color_night;
			radioWrongColor = R.color.radio_wrong_color;
		} else {
			testBgs[0] = R.drawable.test_bg1;
			testBgs[1] = R.drawable.test_bg2;
			testBgs[2] = R.drawable.test_bg3;
			radioTextColor = R.color.black;
			radioWrongColor = R.color.red;
		}

		if (relationShip != null)
			initView();
		return v;
	}

	public void initViewForNewRelationShip(RelationShip relationShip,
			int testMode) {
		this.relationShip = relationShip;
		this.testMode = testMode;
		initView();
	}

	private void initView() {

		pushAnimation = AnimationUtils.loadAnimation(getActivity(),
				R.anim.push_bottom_in);
		pushAnimation.setDuration(400);

		word = wf.getOneWordById(relationShip.getIndex_word_id());

		if (testMode == 3) {
			hintButton.setVisibility(View.INVISIBLE);
			mRelationShipText2.setVisibility(View.VISIBLE);
		} else if (testMode == 4) {
			hintButton.setVisibility(View.VISIBLE);
			mRelationShipText2.setVisibility(View.INVISIBLE);
		}
		// buttonFlipper.setDisplayedChild(0);
		nextButton.setVisibility(View.GONE);
		displayFlipper.setDisplayedChild(0);
		wordErrorContainerParent.setVisibility(View.GONE);

		nextButton.setOnClickListener(this);
		hintButton.setOnClickListener(this);

		for (int i = 0; i < radioButton.length; i++) {
			radioLayout[i].setBackgroundResource(testBgs[0]);
			radioButton[i].setOnClickListener(this);
			radioButton[i]
					.setTextColor(getResources().getColor(radioTextColor));
			radioText[i].setTextColor(getResources().getColor(radioTextColor));
		}

		everforget = false;
		String q = qmaker.getQuestion(relationShip);
		mRelationShipText1.setText(q);
		mRelationShipText2.setText(relationShip.getColloc_cn());
		optionABCD = randomNumBtList(4);

		for (int i = 0; i < 4; i++) {
			String text = "";
			if (i == optionABCD) {
				radioWord[i] = word;
				text = qmaker.getRightChoice(relationShip);
			} else {
				OneWord oneWord = qmaker.getWrongChoice(relationShip);
				radioWord[i] = oneWord;
				text = oneWord.getLemma();
			}

			radioButton[i].setText(text);
			radioButton[i].setEnabled(true);
			resultImage[i].setBackgroundDrawable(null);
		}

		reloadWordListView();

	}

	private void reloadWordListView() {
		rightContainer.removeAllViews();
		rightContainer.addView(CreatView.createWordView(mInflater, word,
				getActivity()));

		CreatView.createChoiceWordView(mInflater, getActivity(),
				wrongContainer, radioWord, optionABCD);
	}

	@Override
	public void onClick(View view) {
		// TODO Auto-generated method stub
		if (view == nextButton) {
			if (everforget)
				mExamRunActivity.next(false);
			else
				mExamRunActivity.next(true);
		} else if (view == hintButton) {
			mRelationShipText2.setVisibility(View.VISIBLE);
			hintButton.setVisibility(View.INVISIBLE);
		} else if ((view == radioButton[0]) || (view == radioButton[1])
				|| (view == radioButton[2]) || (view == radioButton[3])) {
			for (int i = 0; i < radioButton.length; i++)
				if (view == radioButton[i]) {
					if (i == optionABCD) {
						wordErrorContainerParent.setVisibility(View.GONE);
						resultImage[i]
								.setBackgroundResource(R.drawable.right_ico);
						radioLayout[i].setBackgroundResource(testBgs[1]);
						radioButton[i].setTextColor(getResources().getColor(
								R.color.green));
						radioText[i].setTextColor(getResources().getColor(
								R.color.green));
					} else {
						resultImage[i]
								.setBackgroundResource(R.drawable.wrong_ico);
						radioLayout[i].setBackgroundResource(testBgs[2]);
						radioButton[i].setTextColor(getResources().getColor(
								radioWrongColor));
						radioText[i].setTextColor(getResources().getColor(
								radioWrongColor));
						// Toast.makeText(mExamRunActivity, "再选一次",
						// Toast.LENGTH_SHORT).show();

						wordErrorContainer.removeAllViews();
						wordErrorContainer.addView(CreatView.createWordView(
								mInflater, radioWord[i], getActivity()));
						int padding = Utils.changeDipToPx(getActivity(), 10);
						wordErrorContainer.setPadding(padding, 0, padding,
								padding);
						wordErrorContainerParent.startAnimation(pushAnimation);
						wordErrorContainerParent.setVisibility(View.VISIBLE);
					}
					break;
				}

			if (view == radioButton[optionABCD]) {
				handler.postDelayed(new Runnable() {

					@Override
					public void run() {
						if (AppPreference.isAutoPronunciation(getActivity())) {
							VoicePlayer.getInstance(getActivity()).play(word);
						}
						displayFlipper.setDisplayedChild(1);
						// buttonFlipper.setDisplayedChild(1);
						nextButton.setVisibility(View.VISIBLE);
						hintButton.setVisibility(View.GONE);
						mRelationShipText1.setText(relationShip.getColloc_eng());// 设置英文全部都显示出来
						mRelationShipText2.setVisibility(View.VISIBLE);

						for (int i = 0; i < radioButton.length; i++)
							radioButton[i].setEnabled(false);
					}

				}, 0); // 答对后进入搭配详情页面

			} else {
				everforget = true;
			}
		}
	}

	private static int randomNumBtList(int length) {
		int index = 0;
		index = (int) (Math.random() * (length));
		return index;
	}

}